<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	if(!$this->setting['is_open']){
        exi('已关闭，不能参与！','error','/index/');	
    }
	$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
	$this->model('member')->register();
	$return['setting']=$this->setting;
	$redfeeset=$return['setting']['redfeeset'];
	$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($member['nocheck']){
		exi('您当前账户属于未审核状态，不能参与！','error','/index/');	
	}
	$apilog=model('core/member/cash')->getlog('',$this->setting['is_mobileshi']);
	if($apilog['passed']!=1 && $return['setting']['is_bankopen']){
		exi('您要先实名绑定银行卡，才能操作！','error','core/member.mybank/');
	}
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);
	if($member['agent_id']){
		$member['agent']=model('core/member')->basic($member['agent_id']);
	}else{
		$member['agent']=array('nickname'=>'总店');
	}
	if($member['level_id']){
		$member['level']=$this->get('level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['level_id']));
	}else{
		$member['level']=$this->plugin_setting('default_level');
	}
	$return['member']=$member;
	$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('credit2'));
	$return['item']['money']=$redfeeset[0]['fee'];
	$return['fee1']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>1),'money');
	$return['maxcong']=pdo_get('jy_cun9fan_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','price desc');
	$return['redfeeset']=$redfeeset;
	$return['xieyi']=$this->plugin_setting('xieyi2');
	if(!$return['xieyi']['is_tan']){
		$return['item']['check']=1;
	}
	exi($return);
}
if($_RQ['op']=='recharge'){
	$fanlog = $this->pdo_get('fanlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(0,2)));
	if($fanlog['id']){
		exi('您当前还有一单正在回补中！不能购买！','error');
	}
	$post=get_postdata();
	if(intval($post['money'])<=0){
		exi('请先设置充值金额！并且为整数！','error');
	}
	$fee=intval($post['money']);
	$maxcong=pdo_get('jy_cun9fan_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','price desc');
	if($maxcong['price'] && $maxcong['price']>$fee){
		exi('本次充值必须大于等于'.$maxcong['price'].'额度！','error');
	}
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'fee'=>$fee,
		'pluginname'=>'jy_cun9fan',
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'余额充值',
		'plugin'=>'core',
		//'pay_type'=>'wechat',
		'no_type'=>$this->setting['is_fee2']?array('hdfk'):array('hdfk','credit'),
		'back_url'=>'jy_cun9fan/recharge/pay_result',
		'success_url'=>'core/member.log/',
		'error_url'=>'core/member.log/',
	);
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$recharge=pdo_get('core_recharge',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($recharge['fee']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('core_recharge',array('status'=>1,'pay_type'=>$payresult['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
	if($this->setting['is_songbi']){
		$jifenfee=$recharge['fee'];
		foreach($this->setting['redfeeset'] as $rv){
			if($rv['fee']==$recharge['fee'] && $rv['jifenfee']>0){
				$jifenfee=$rv['jifenfee'];
				$recharge['bei']=$rv['bei'];
			}
		}
		cfc('core')->member_credit($recharge['mid'],'credit3',$jifenfee,1,'jy_cun9fan','充值送金币');
		if($this->setting['is_duibi']){
			$recharge['jifenfee']=$jifenfee;
		}
	}
	$this->model('commission')->create($recharge);
	exi('充值成功！');
}